package com.tyqx.hashchainbackend.service.impl;


import com.tyqx.hashchainbackend.service.SysRolePermissionService;
import com.tyqx.hashchainbean.module.SysRolePermission;
import com.tyqx.hashchainbean.vo.IdVO;
import com.tyqx.hashchainbean.vo.PerOrResourceParamVO;
import com.tyqx.hashchainbean.vo.PerOrResourceVO;
import com.tyqx.hashchainbean.vo.SysRolePermissionVO;
import com.tyqx.hashchaindao.mapper.SysRolePermissionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("sysRolePermissionService")
public class SysRolePermissionServiceImpl implements SysRolePermissionService {

    @Resource
    SysRolePermissionMapper sysRolePermissionMapper;

    @Override
    public SysRolePermission getById(Long rpId) throws Exception {
        return null;
    }

    /**
     * 查询角色已拥有的权限
     *
     * @param roleId 角色ID
     * @return
     * @throws Exception
     */
    @Override
    public List<PerOrResourceParamVO> queryRolePermission(Long roleId) throws Exception {
        Map<String,Object> param = new HashMap<>();
        param.put("rpRoleId",roleId);
        return sysRolePermissionMapper.getListByMap(param);
    }


    @Override
    public Integer getCountByMap(Map<String, Object> param) throws Exception {
        return null;
    }

    @Override
    public boolean save(IdVO idVO) throws Exception {
        List<PerOrResourceVO> perOrResourceVOS = new ArrayList<>();
        for (Long id : idVO.getPreIdList()) {
            PerOrResourceVO perOrResourceVO = new PerOrResourceVO();
            perOrResourceVO.setCreationDate(new Date());
            perOrResourceVO.setCreateBy(idVO.getUserId().toString());
            perOrResourceVO.setRoleId(idVO.getRoleId());
            perOrResourceVO.setPerOrRrId(id);
            perOrResourceVOS.add(perOrResourceVO);
        }
        return sysRolePermissionMapper.save(perOrResourceVOS) > 0 ? true : false;
    }

    @Override
    public boolean modify(SysRolePermission sysRolePermission) throws Exception {
        return false;
    }

    @Override
    public Integer removeByrpRoleId(Long rpRoleId) throws Exception {
        return null;
    }


    public List<SysRolePermissionVO> getListByMap(Integer roleId) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("rpRoleId", roleId);
        return sysRolePermissionMapper.getListByMaps(param);
    }

    @Override
    public List<SysRolePermissionVO> getList(Integer roleId) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("rpRoleId", roleId);
        return sysRolePermissionMapper.getListByMaps(param);
    }

    @Override
    public boolean removeById(Long roleId, List<Long> perIds) throws Exception {
        for (Long id: perIds) {
            sysRolePermissionMapper.removeByRoleAndPerId(roleId,id);
        }
        return true;
    }
}
